{extends file="../home.html"}
{block name="title"}{$head_title}{/block}
{block name="css"}
	<link rel="stylesheet" type="text/css" href="__PUBLIC__/Css/front/front_css/detail_page.css?version={$version}"/>
{/block}
{block name="content"}
	<div class="deliver-header">
		<span class="clearfix mui-action-back" onclick="return_last_page();">
			<i class="icon icon_back_default3x back-icon fl"></i>
			<span class="back-word fl">返回</span>
		</span>
		<h1>发表观点</h1>
		<div class="deliver-btn">发布</div>
	</div>
	<div class="deliver-cont">
		<textarea name="" placeholder="快来发表自己的观点吧~" class="deliver-txt"></textarea>
	</div>
	<div class="up_img clearfix">
			<div class="preview fl clearfix img_html">
				<div class="up_img_box fl">
			        <a href="javascript:;" class="add icon fl icon_upload_default3x" title="上传一张新的图片" id="add_upload"></a>
			        <input type="hidden" name="img_url " class="img_url" id="img_url" />
			    </div>
			</div>
		    
		</div>
{/block}
{block name="js"}

	<script type="text/javascript" src="/Public/Js/front/front_js/ajaxupload.3.5.js"></script>
	<script type="text/javascript">
	var topic_id = '{$topic_id}'
	{literal}
		$(function(){
			$(".deliver-header i,.deliver-header span").on("touchstart",function(){
				history.go(-1);
			});
			$(".deliver-btn").on("click",function(){
				var contents = $(".deliver-txt").val();
				if(contents==""){
                    layer.open({
                        content: '请输入发表内容',
                        title: false,
                        btn: ['确定'],
                    });
                    return;
                }
                var imgs = $("#img_url").val();
                $.ajax({
                	url:'/FrontEnjoy/add_view',
                	type:'post',
                	data:{topic_id:topic_id, contents:contents, imgs:imgs},
                	success:function(r){
                		if(r.code == 0){
                			layer.open({
		                        content: r.msg,
		                        title: false,
		                        btn: ['确定'],
								yes:function(){
									if(!is_wechat()){
                						native_listen('back');
                					}else{
                						history.go(-1);
                					}

									//mui.back();
									//history.go(-1);
									//location.replace('/FrontEnjoy/topic_details/topic_id/'+topic_id)
									//location.href = '/FrontEnjoy/topic_details/topic_id/'+topic_id;
								}
		                    });
							//history.go(-1);
                			//location.href = '/FrontEnjoy/topic_details/topic_id/'+topic_id;
                		}else{
                			layer.open({
		                        content: r.msg,
		                        title: false,
		                        btn: ['确定'],
		                    });
                		}
                	}
                });
			});
            // 上传图片
            new AjaxUpload("#add_upload", {
	            action: "/Front/uploadHandler",
	            name: "upfile",
	            responseType: "json",
	            onSubmit: function(){
	            	//alert('正在上传');
	                //preview处的图片改为loading图片
	                $('.preview').show();
	                if($("#add_upload").parents('.preview').find('img').length>=9){
	                    //alert("最多上传9张图片");
	                    layer.open({
		                        content: "最多上传9张图片",
		                        title: false,
		                        btn: ['确定'],
		                    });
	                    return false;
	                }
	            },
	            onChange: function(file, extension){
	                if (extension && /^(jpg|png|jpeg|gif)$/.test(extension)) {
	                    return true;
	                }
	                else {
	                    //alert('暂不支持该图片格式！');
	                    layer.open({
		                        content: "暂不支持该图片格式",
		                        title: false,
		                        btn: ['确定'],
		                    });
	                    return false;
	                }
	            },
	            onComplete: function(file, response){
	            //  console.log(response);
	                if (response.status === 0) {
	                    //alert(response.msg);
	                    layer.open({
		                        content: response.msg,
		                        title: false,
		                        btn: ['确定'],
		                    });
	                }
	                else if (response.status === 1) {
	                    var html;
	                    html='<div class=" fl img_box"><img class="pic-preview" src="'+response.img_url+'"/><div class="del_img"></div></div>';
	                    var img_url = $("#img_url").val();
	                    
	                    $("#img_url").val(img_url+','+response.img_url);
	                   
	                    $('.preview').prepend(html);
	                    //$("#img_url").val(response.img_url);
	                    $(".del_img").on("click",function(){
			                if($(this).parents(".preview").find('img').length==1){
			                	/*console.log(1);
			                    $(this).parents(".img_html").siblings('.img_url').val('');      */
			                }
			                $(this).parent(".img_box").remove();
			                var pics = '';
			                console.log($('.preview').find('img').length)
			                $('.preview').find('img').each(function(){
			                    pics +=  ','+$(this).attr("src");
			                });
			            });
	                }
	            }
            });
		});
	{/literal}
	</script>

{/block}
